"use strict";

/**
 * add连接
 * @param {Criteria} cri 
 * @param {String} term 
 */
function $and(cri, term) {
  if (!cri.terms || cri.terms == "") {
    cri.terms += term;
  } else {
    cri.terms += " and " + term;
  }
}

/**
 * sql条件
 */
class Criteria {
  constructor() {
    this.vals = [];
    this.terms = "";
  }

  /**
   * 清除条件
   */
  clear() {
    this.vals = [];
    this.terms = "";
  }
  /**
  * 等于
  * @param {String} field 
  * @param {*} val 
  */
  andEqual(field, val) {
    $and(this, field + " = ?");
    this.vals.push(val);
    return this;
  }

  /**
   * 不等于
   * @param {String} field 
   * @param {*} val 
   */
  andNotEqual(field, val) {
    $and(this, field + " != ?");
    this.vals.push(val);
    return this;
  }

  /**
   * 模糊条件
   * @param {String} field 
   * @param {*} val 
   */
  andLike(field, val) {
    $and(this, field + " like ?");
    this.vals.push(val);
    return this;
  }

  /**
   * 大于
   * @param {String} field 
   * @param {*} val 
   */
  andGreaterThan(field, val) {
    $and(this, field + " > ?");
    this.vals.push(val);
    return this;
  }

  /**
   * 大于等于
   * @param {String} field 
   * @param {*} val 
   */
  andGreaterEqual(field, val) {
    $and(this, field + " >= ?");
    this.vals.push(val);
    return this;
  }

  /**
  * 小于
  * @param {String} field 
  * @param {*} val 
  */
  andLessThan(field, val) {
    $and(this, field + " < ?");
    this.vals.push(val);
    return this;
  }

  /**
  * 小于等于
  * @param {String} field 
  * @param {*} val 
  */
  andLessEqual(field, val) {
    $and(this, field + " <= ?");
    this.vals.push(val);
    return this;
  }
}
module.exports = Criteria;